package cz.uvn.orthopedy.server.jdo;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import cz.uvn.orthopedy.client.entity.ScheduleEntry;

public final class ScheduleJdo {
	
	private ScheduleJdo() {
	}
	
	public static void editScheduleEntry(ScheduleEntry entry) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Query query = pm.newQuery(ScheduleEntry.class);
			query.setFilter("id == idParam");
			query.declareParameters("Long idParam");
			query.setUnique(true);
			ScheduleEntry result = (ScheduleEntry) query.execute(entry.getId());
			if (result != null) {
				result.setOwnerId(entry.getOwnerId());
				result.setDay(entry.getDay());
				result.setHallId(entry.getHallId());
				result.setType(entry.getType());
				result.setOperationType(entry.getOperationType());
			}
		} finally {
			pm.close();
		}
	}

	public static Long createScheduleEntry(ScheduleEntry entry) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			ScheduleEntry result = pm.makePersistent(entry);
			return result.getId();
		} finally {
			pm.close();
		}
	}
	
	public static void deleteScheduleEntry(ScheduleEntry entry) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Query query = pm.newQuery(ScheduleEntry.class);
			query.setFilter("id == idParam");
			query.declareParameters("Long idParam");
			@SuppressWarnings("unchecked")
			List<ScheduleEntry> result = (List<ScheduleEntry>) query.execute(entry.getId());
			assert result.size() == 1;
			pm.deletePersistent(result.get(0));
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public static ScheduleEntry[] getSchedule(int day) {
		ScheduleEntry[] schedule = null;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		pm.setDetachAllOnCommit(true);
		try {
			Query q = pm.newQuery(ScheduleEntry.class);
			q.setOrdering("hallId"); // TODO hall
			q.setFilter("day == dayParam");
			q.declareParameters("int dayParam");
			List<ScheduleEntry> result = (List<ScheduleEntry>) q.execute(day);
			schedule = new ScheduleEntry[result.size()];
			for (int i = 0; i < result.size(); i++) {
				ScheduleEntry e = result.get(i);
				schedule[i] = new ScheduleEntry(e.getId(), e.getOwnerId(), e.getDay(),
						e.getHallId(), e.getType(), e.getOperationType());
			}
		} finally {
			pm.close();
		}
		return schedule;
	}

	public static void initSchedule(ScheduleEntry[] entries) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			for (ScheduleEntry entry : entries) {
				Long hallId = entry.getHallId();
				if (hallId != null) {
					entry.setHallId(HallJdo.getHall(hallId).getId());
				}
				
				Long doctorId = entry.getOwnerId();
				if (doctorId != null) {
					entry.setOwnerId(DoctorJdo.getDoctor(doctorId).getId());
				}
				
				pm.makePersistent(entry);
			}
		} finally {
			pm.close();
		}
	}

	public static ScheduleEntry[] getScheduleEntries() {
		ScheduleEntry[] entries = null;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		pm.setDetachAllOnCommit(true);
		try {
			Query q = pm.newQuery(ScheduleEntry.class);
			q.setOrdering("day");
			@SuppressWarnings("unchecked")
			List<ScheduleEntry> result = (List<ScheduleEntry>) q.execute();
			entries = new ScheduleEntry[result.size()];
			for (int i = 0; i < result.size(); i++) {
				ScheduleEntry e = result.get(i);
				entries[i] = new ScheduleEntry(e.getId(), e.getOwnerId(), e.getDay(), e.getHallId(), e.getType(), e.getOperationType());
			}
		} finally {
			pm.close();
		}
		return entries;
	}
	

}
